//
//  BLClothAddPresntViewController.m
//  blanket
//
//  Created by Mac on 2017/12/6.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLClothAddPresntViewController.h"
#import "BLTextFieldControl.h"
@interface BLClothAddPresntViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
//床单1.8 -1.5 -1.2
@property(nonatomic, strong) BLTextFieldControl                     *bed_one_meter_eight;
@property(nonatomic, strong) BLTextFieldControl                     *bed_one_meter_five;
@property(nonatomic, strong) BLTextFieldControl                     *bed_one_meter_two;
//b被子1.8 -1.5 -1.2
@property(nonatomic, strong) BLTextFieldControl                     *quilt_one_meter_eight;
@property(nonatomic, strong) BLTextFieldControl                     *quilt_one_meter_five;
@property(nonatomic, strong) BLTextFieldControl                     *quilt_one_meter_two;

@property(nonatomic, strong) BLTextFieldControl                     *tick;//枕套
@property(nonatomic, strong) BLTextFieldControl                     *face_cloth;//面巾
@property(nonatomic, strong) BLTextFieldControl                     *towel;//yu'jin

@end

@implementation BLClothAddPresntViewController
- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth - 60, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    self.customView.layer.cornerRadius = 10;
    self.customView.clipsToBounds = YES;
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
    NSString * title = _isClothAdd?@"请输入需要增加的库存量":@"请输入需要减少的库存量";
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(20, 50, self.customView.width - 40, 20)
                                                     text:title
                                                textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [self.customView addSubview:titleLable];
    CGFloat controlWith = (self.customView.width - 60)/2;
    BLTextFieldControl * chuanDan = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20, titleLable.bottom + 30, controlWith, 25) leftTitle:@"床单1.8:" rightTitle:@""];
    [self.customView addSubview:chuanDan];
    self.bed_one_meter_eight = chuanDan;
    
    BLTextFieldControl * beitao = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20 + chuanDan.right, titleLable.bottom + 30, controlWith, 25) leftTitle:@"床单1.5:" rightTitle:@""];
    [self.customView addSubview:beitao];
    self.bed_one_meter_five = beitao;
    
    BLTextFieldControl * maojin = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20, chuanDan.bottom + 20, controlWith, 25) leftTitle:@"床单1.2:" rightTitle:@""];
    [self.customView addSubview:maojin];
    self.bed_one_meter_two = maojin;
    
    BLTextFieldControl * didian = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20+chuanDan.right , chuanDan.bottom + 20, controlWith, 25) leftTitle:@"被套1.8:" rightTitle:@""];
    [self.customView addSubview:didian];
    self.quilt_one_meter_eight = didian;
    
    
    BLTextFieldControl * quilt_one_meter_five = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20 , maojin.bottom + 20, controlWith, 25) leftTitle:@"被套1.5:" rightTitle:@""];
    [self.customView addSubview:quilt_one_meter_five];
    self.quilt_one_meter_five = quilt_one_meter_five;

    
    BLTextFieldControl * quilt_one_meter_two = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20+chuanDan.right , maojin.bottom + 20, controlWith, 25) leftTitle:@"被套1.2:" rightTitle:@""];
    [self.customView addSubview:quilt_one_meter_two];
    self.quilt_one_meter_two = quilt_one_meter_two;
    
    
    BLTextFieldControl * tick = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20 , quilt_one_meter_two.bottom + 20, controlWith, 25) leftTitle:@"枕套:" rightTitle:@""];
    [self.customView addSubview:tick];
    self.tick = tick;
    
    
    BLTextFieldControl * face_cloth = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20+chuanDan.right , quilt_one_meter_two.bottom + 20, controlWith, 25) leftTitle:@"面巾:" rightTitle:@""];
    [self.customView addSubview:face_cloth];
    self.face_cloth = face_cloth;
    
    
    BLTextFieldControl * towel = [[BLTextFieldControl alloc] initWithFrame:CGRectMake(20 , face_cloth.bottom + 20, controlWith, 25) leftTitle:@"浴巾:" rightTitle:@""];
    [self.customView addSubview:towel];
    self.towel = towel;
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(60, towel.bottom + 60, self.customView.width - 120, 44)
                                                        title:@"确定"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];

    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.customView addSubview:confimButton];

    self.customView.height = confimButton.bottom + 80;
    
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.centerY = UIScreenHeight / 2 ;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    if (!self.bed_one_meter_eight.textFiled.text.length) {
        self.bed_one_meter_eight.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入床单1.8数量"];
//        return;
    }
    if (!self.bed_one_meter_five.textFiled.text.length) {
        self.bed_one_meter_five.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入床单1.5数量"];
//        return;
    }
    if (!self.bed_one_meter_two.textFiled.text.length) {
        self.bed_one_meter_two.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入床单1.2数量"];
//        return;
    }
    
    if (!self.quilt_one_meter_eight.textFiled.text.length) {
        self.quilt_one_meter_eight.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入被套1.8数量"];
//        return;
    }
    if (!self.quilt_one_meter_five.textFiled.text.length) {
         self.quilt_one_meter_five.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入被套1.5数量"];
//        return;
    }
    if (!self.quilt_one_meter_two.textFiled.text.length) {
        self.quilt_one_meter_two.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入被套1.2数量"];
//        return;
    }
    
    
    if (!self.face_cloth.textFiled.text.length) {
        self.face_cloth.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入面巾数量"];
//        return;
    }
    if (!self.tick.textFiled.text.length) {
        self.tick.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入枕套数量"];
//        return;
    }
    if (!self.towel.textFiled.text.length) {
         self.towel.textFiled.text = @"0";
//        [SVProgressHUD showErrorWithStatus:@"请输入浴巾数量"];
//        return;
    }
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * type = self.isClothAdd?@"1":@"2";
    [dic setObject:type forKey:@"type"];
    if ([STUserManger defult].userID.length) {
         [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [dic setObject:debugToken forKey:@"token"];
    
    [dic setObject:self.bed_one_meter_eight.textFiled.text forKey:@"bed_one_meter_eight"];
    [dic setObject:self.bed_one_meter_five.textFiled.text forKey:@"bed_one_meter_five"];
    [dic setObject:self.bed_one_meter_two.textFiled.text forKey:@"bed_one_meter_two"];
    
    [dic setObject:self.quilt_one_meter_eight.textFiled.text forKey:@"quilt_one_meter_eight"];
    [dic setObject:self.quilt_one_meter_five.textFiled.text forKey:@"quilt_one_meter_five"];
    [dic setObject:self.quilt_one_meter_two.textFiled.text forKey:@"quilt_one_meter_two"];
    
    [dic setObject:self.tick.textFiled.text forKey:@"tick"];
    [dic setObject:self.face_cloth.textFiled.text forKey:@"face_cloth"];
    [dic setObject:self.towel.textFiled.text forKey:@"towel"];
    
    BLRepertoryModel * model = [BLRepertoryModel mj_objectWithKeyValues:dic];
    if (_originModel && model && !self.isClothAdd) {
        //减少布草需要判断
        if (_originModel.bed_one_meter_two.floatValue < model.bed_one_meter_two.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.bed_one_meter_eight.floatValue < model.bed_one_meter_eight.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.bed_one_meter_five.floatValue < model.bed_one_meter_five.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.quilt_one_meter_eight.floatValue < model.bed_one_meter_two.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.quilt_one_meter_five.floatValue < model.bed_one_meter_two.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.quilt_one_meter_two.floatValue < model.bed_one_meter_two.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.tick.floatValue < model.tick.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.face_cloth.floatValue < model.face_cloth.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
        
        if (_originModel.towel.floatValue < model.towel.floatValue ) {
            [SVProgressHUD showErrorWithStatus:@"减少数量不能大于库存"];
            return;
        }
    }
    if (self.finshActionHandle) {
        __weak typeof(self) weakSelf =  self;
        self.finshActionHandle(dic,weakSelf);
    }
}
- (void)onSelectedWhiteView{
     [self.view endEditing:YES];
}
@end
